{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 1. 文档\n",
    "\n",
    "案例大全：https://www.windquant.com/qntcloud/apiTemplateMenu/id-d1d00878-8e85-40d3-bf91-bde9401f0b8a\n",
    "\n",
    "帮助文档：https://www.windquant.com/qntcloud/apiRefHelp/id-91573a98-70d5-4462-8c6f-546ab45c8652\n",
    "\n",
    "注意事项：\n",
    "* 每次限8000单元格\n",
    "* 数据流量的计算方法是滚动计算，从当前时间前推7*24小时，这段时间内的用量超过了限制，\n",
    "  则需要过一段时间再用，等到按前面所说计算方法计算出来的用量不再超过限制的流量。 \n",
    "  具体每个函数的流量限制，请联系客户经理咨询"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "Welcome to use Wind Quant API for Python (WindPy)!\n",
      "\n",
      "COPYRIGHT (C) 2020 WIND INFORMATION CO., LTD. ALL RIGHTS RESERVED.\n",
      "IN NO CIRCUMSTANCE SHALL WIND BE RESPONSIBLE FOR ANY DAMAGES OR LOSSES CAUSED BY USING WIND QUANT API FOR Python.\n"
     ],
     "output_type": "stream"
    },
    {
     "data": {
      "text/plain": "True"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 1
    }
   ],
   "source": [
    "# 1. 连接万得服务\n",
    "from WindPy import w\n",
    "\n",
    "w.start() # 默认命令超时时间为120秒，如需设置超时时间可以加入waitTime参数，例如waitTime=60,即设置命令超时时间为60秒  \n",
    "w.isconnected() # 判断WindPy是否已经登录成功\n",
    "# w.stop() # 当需要停止WindPy时，可以使用该命令\n",
    "          # 注： w.start不重复启动，若需要改变参数，如超时时间，用户可以使用w.stop命令先停止后再启动。\n",
    "          # 退出时，会自动执行w.stop()，一般用户并不需要执行w.stop "
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2. 获取股票列表\n",
    "\n",
    "对应接口“获取报表数据函数WSET”"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "        date  wind_code sec_name\n1 2022-09-20  000001.SZ     平安银行\n2 2022-09-20  000002.SZ      万科A\n3 2022-09-20  000004.SZ     ST国华\n4 2022-09-20  000005.SZ     ST星源\n5 2022-09-20  000006.SZ     深振业A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>date</th>\n      <th>wind_code</th>\n      <th>sec_name</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>1</td>\n      <td>2022-09-20</td>\n      <td>000001.SZ</td>\n      <td>平安银行</td>\n    </tr>\n    <tr>\n      <td>2</td>\n      <td>2022-09-20</td>\n      <td>000002.SZ</td>\n      <td>万科A</td>\n    </tr>\n    <tr>\n      <td>3</td>\n      <td>2022-09-20</td>\n      <td>000004.SZ</td>\n      <td>ST国华</td>\n    </tr>\n    <tr>\n      <td>4</td>\n      <td>2022-09-20</td>\n      <td>000005.SZ</td>\n      <td>ST星源</td>\n    </tr>\n    <tr>\n      <td>5</td>\n      <td>2022-09-20</td>\n      <td>000006.SZ</td>\n      <td>深振业A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 3
    }
   ],
   "source": [
    "# wset函数支持输出DataFrame数据格式，需要函数添加参数usedf = True, 如例2。可以使用usedfdt=True来填充DataFrame输出NaT的日期\n",
    "# sectorconstituent：板块成分\n",
    "\n",
    "# 获取A股所有列表\n",
    "all_a = w.wset(\"SectorConstituent\",date = '2022-09-20' ,sector=u\"全部A股\", usedf = True)\n",
    "all_a[1].head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "   wind_code sec_name\n1  000001.SZ     平安银行\n2  000002.SZ      万科A\n3  000060.SZ     中金岭南\n4  000063.SZ     中兴通讯\n5  000069.SZ     华侨城A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>wind_code</th>\n      <th>sec_name</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>1</td>\n      <td>000001.SZ</td>\n      <td>平安银行</td>\n    </tr>\n    <tr>\n      <td>2</td>\n      <td>000002.SZ</td>\n      <td>万科A</td>\n    </tr>\n    <tr>\n      <td>3</td>\n      <td>000060.SZ</td>\n      <td>中金岭南</td>\n    </tr>\n    <tr>\n      <td>4</td>\n      <td>000063.SZ</td>\n      <td>中兴通讯</td>\n    </tr>\n    <tr>\n      <td>5</td>\n      <td>000069.SZ</td>\n      <td>华侨城A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 7
    }
   ],
   "source": [
    "# 获取沪深300所有列表\n",
    "stock_codes = w.wset(\"sectorconstituent\", \"date=2018-06-12;windcode=000300.SH;field=wind_code,sec_name\", usedf = True)\n",
    "stock_codes[1].head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "   wind_code  sec_name\n1  801010.SI  农林牧渔(申万)\n2  801030.SI  基础化工(申万)\n3  801040.SI    钢铁(申万)\n4  801050.SI  有色金属(申万)\n5  801080.SI    电子(申万)",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>wind_code</th>\n      <th>sec_name</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>1</td>\n      <td>801010.SI</td>\n      <td>农林牧渔(申万)</td>\n    </tr>\n    <tr>\n      <td>2</td>\n      <td>801030.SI</td>\n      <td>基础化工(申万)</td>\n    </tr>\n    <tr>\n      <td>3</td>\n      <td>801040.SI</td>\n      <td>钢铁(申万)</td>\n    </tr>\n    <tr>\n      <td>4</td>\n      <td>801050.SI</td>\n      <td>有色金属(申万)</td>\n    </tr>\n    <tr>\n      <td>5</td>\n      <td>801080.SI</td>\n      <td>电子(申万)</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 5
    }
   ],
   "source": [
    "# 获取申万一级行业的成分股\n",
    "sw_index=w.wset(\"sectorconstituent\",\"date=2018-06-12;sectorid=a39901011g000000;field=wind_code,sec_name\",usedf=True)\n",
    "sw_index[1].head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "        date  wind_code sec_name suspend_type  \\\n1 2022-08-22  000670.SZ      盈方微         盘中停牌   \n2 2022-08-22  000972.SZ     ST中基         停牌一天   \n3 2022-08-22  300018.SZ     中元股份         今起停牌   \n4 2022-08-22  300419.SZ     浩丰科技         今起停牌   \n5 2022-08-22  300521.SZ      爱司凯         停牌一天   \n\n                                suspend_reason  \n1  盘中成交价较开盘价首次上涨达到或超过10%，盘中成交价较开盘价首次上涨达到或超过20%  \n2                                         重大事项  \n3                                         重大事项  \n4                                         重大事项  \n5                                         重大事项  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>date</th>\n      <th>wind_code</th>\n      <th>sec_name</th>\n      <th>suspend_type</th>\n      <th>suspend_reason</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>1</td>\n      <td>2022-08-22</td>\n      <td>000670.SZ</td>\n      <td>盈方微</td>\n      <td>盘中停牌</td>\n      <td>盘中成交价较开盘价首次上涨达到或超过10%，盘中成交价较开盘价首次上涨达到或超过20%</td>\n    </tr>\n    <tr>\n      <td>2</td>\n      <td>2022-08-22</td>\n      <td>000972.SZ</td>\n      <td>ST中基</td>\n      <td>停牌一天</td>\n      <td>重大事项</td>\n    </tr>\n    <tr>\n      <td>3</td>\n      <td>2022-08-22</td>\n      <td>300018.SZ</td>\n      <td>中元股份</td>\n      <td>今起停牌</td>\n      <td>重大事项</td>\n    </tr>\n    <tr>\n      <td>4</td>\n      <td>2022-08-22</td>\n      <td>300419.SZ</td>\n      <td>浩丰科技</td>\n      <td>今起停牌</td>\n      <td>重大事项</td>\n    </tr>\n    <tr>\n      <td>5</td>\n      <td>2022-08-22</td>\n      <td>300521.SZ</td>\n      <td>爱司凯</td>\n      <td>停牌一天</td>\n      <td>重大事项</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 13
    }
   ],
   "source": [
    "# 获取所有停牌股票\n",
    "st_codes = w.wset(\"tradesuspend\",\"startdate=2022-08-22;enddate=2022-09-22;\",usedf=True)\n",
    "st_codes[1].head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3. 板块信息获取"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "        code  name  count      marketcap  freesharesmv   weight     point\n1  882007.WI    金融     49  130707.910205  36800.309687  20.1791 -3.691025\n2  882011.WI   房地产      6    6692.101775   3522.772276   1.8438 -1.295100\n3  882008.WI  信息技术     61   51289.954607  27329.989327  16.1443 -4.020317\n4  882004.WI  可选消费     25   34377.825323  13477.991229   8.1729 -5.773307\n5  882003.WI    工业     47   58939.112800  25509.596964  15.4582 -4.002825",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>code</th>\n      <th>name</th>\n      <th>count</th>\n      <th>marketcap</th>\n      <th>freesharesmv</th>\n      <th>weight</th>\n      <th>point</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>1</td>\n      <td>882007.WI</td>\n      <td>金融</td>\n      <td>49</td>\n      <td>130707.910205</td>\n      <td>36800.309687</td>\n      <td>20.1791</td>\n      <td>-3.691025</td>\n    </tr>\n    <tr>\n      <td>2</td>\n      <td>882011.WI</td>\n      <td>房地产</td>\n      <td>6</td>\n      <td>6692.101775</td>\n      <td>3522.772276</td>\n      <td>1.8438</td>\n      <td>-1.295100</td>\n    </tr>\n    <tr>\n      <td>3</td>\n      <td>882008.WI</td>\n      <td>信息技术</td>\n      <td>61</td>\n      <td>51289.954607</td>\n      <td>27329.989327</td>\n      <td>16.1443</td>\n      <td>-4.020317</td>\n    </tr>\n    <tr>\n      <td>4</td>\n      <td>882004.WI</td>\n      <td>可选消费</td>\n      <td>25</td>\n      <td>34377.825323</td>\n      <td>13477.991229</td>\n      <td>8.1729</td>\n      <td>-5.773307</td>\n    </tr>\n    <tr>\n      <td>5</td>\n      <td>882003.WI</td>\n      <td>工业</td>\n      <td>47</td>\n      <td>58939.112800</td>\n      <td>25509.596964</td>\n      <td>15.4582</td>\n      <td>-4.002825</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 6
    }
   ],
   "source": [
    "# 成分行业分布: 仅有11个\n",
    "# 输出字段：code: 代码，name：行业名称，count: 成份个数，marketcap:总市值，\n",
    "#   freesharesmv:自由流通市值，weight: 权重(%)，point: 贡献点\n",
    "sec_industry = w.wset(\"constituentsindustrydist\",\"windcode=000300.SH;enddate=2022-09-22;industry=Wind一级行业\",usedf=True)\n",
    "sec_industry[1].head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "                  SEC_CLOSE_AVG\na001010200000000      13.520050\na001010300000000      13.270491",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>SEC_CLOSE_AVG</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>a001010200000000</td>\n      <td>13.520050</td>\n    </tr>\n    <tr>\n      <td>a001010300000000</td>\n      <td>13.270491</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 9
    }
   ],
   "source": [
    "# 这些ID只能在代码生成器中去查，没有接口获取ID和对应中文的列表\n",
    "# 提取上证A股和深证A股的当日平均收盘价信息。\n",
    "errorCode,data=w.wsee(\"a001010200000000,a001010300000000\",\"sec_close_avg\",\"tradeDate=20180827\",usedf=True)\n",
    "data.head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 4. 获取行情数据\n",
    "## 4.1 历史日行情数据查询"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "                  OPEN        HIGH         LOW       CLOSE      VOLUME   CHG  \\\n2022-08-22  106.728977  107.620862  106.431682  107.472215  13776616.0  0.03   \n2022-08-23  107.174920  107.323567  106.580329  106.580329  15047471.0 -0.06   \n2022-08-24  106.580329  107.323567  106.134386  106.283034  16855765.0 -0.02   \n2022-08-25  106.431682  107.323567  106.283034  107.026272  15772556.0  0.05   \n2022-08-26  107.472215  107.918158  106.728977  107.323567  17576235.0  0.02   \n\n                    AMT        VWAP   PRE_CLOSE  ADJFACTOR      TURN  \\\n2022-08-22   99353492.0  107.200913  107.026272   14.86476  0.046936   \n2022-08-23  108111340.0  106.798619  107.472215   14.86476  0.051265   \n2022-08-24  120996053.0  106.703985  106.580329   14.86476  0.057426   \n2022-08-25  113287117.0  106.766830  106.283034   14.86476  0.053736   \n2022-08-26  126876441.0  107.303290  107.026272   14.86476  0.059881   \n\n           TRADE_STATUS SUSP_REASON  MAXUPORDOWN  MAXUP  MAXDOWN  \\\n2022-08-22           交易        None            0   7.92     6.48   \n2022-08-23           交易        None            0   7.95     6.51   \n2022-08-24           交易        None            0   7.89     6.45   \n2022-08-25           交易        None            0   7.87     6.44   \n2022-08-26           交易        None            0   7.92     6.48   \n\n            OPEN_AUCTION_PRICE  OPEN_AUCTION_VOLUME  OPEN_AUCTION_AMOUNT  \n2022-08-22                7.18             422253.0           3031776.54  \n2022-08-23                7.21              60300.0            434763.00  \n2022-08-24                7.17              71877.0            515358.09  \n2022-08-25                7.16              57900.0            414564.00  \n2022-08-26                7.23             187100.0           1352733.00  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>OPEN</th>\n      <th>HIGH</th>\n      <th>LOW</th>\n      <th>CLOSE</th>\n      <th>VOLUME</th>\n      <th>CHG</th>\n      <th>AMT</th>\n      <th>VWAP</th>\n      <th>PRE_CLOSE</th>\n      <th>ADJFACTOR</th>\n      <th>TURN</th>\n      <th>TRADE_STATUS</th>\n      <th>SUSP_REASON</th>\n      <th>MAXUPORDOWN</th>\n      <th>MAXUP</th>\n      <th>MAXDOWN</th>\n      <th>OPEN_AUCTION_PRICE</th>\n      <th>OPEN_AUCTION_VOLUME</th>\n      <th>OPEN_AUCTION_AMOUNT</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>2022-08-22</td>\n      <td>106.728977</td>\n      <td>107.620862</td>\n      <td>106.431682</td>\n      <td>107.472215</td>\n      <td>13776616.0</td>\n      <td>0.03</td>\n      <td>99353492.0</td>\n      <td>107.200913</td>\n      <td>107.026272</td>\n      <td>14.86476</td>\n      <td>0.046936</td>\n      <td>交易</td>\n      <td>None</td>\n      <td>0</td>\n      <td>7.92</td>\n      <td>6.48</td>\n      <td>7.18</td>\n      <td>422253.0</td>\n      <td>3031776.54</td>\n    </tr>\n    <tr>\n      <td>2022-08-23</td>\n      <td>107.174920</td>\n      <td>107.323567</td>\n      <td>106.580329</td>\n      <td>106.580329</td>\n      <td>15047471.0</td>\n      <td>-0.06</td>\n      <td>108111340.0</td>\n      <td>106.798619</td>\n      <td>107.472215</td>\n      <td>14.86476</td>\n      <td>0.051265</td>\n      <td>交易</td>\n      <td>None</td>\n      <td>0</td>\n      <td>7.95</td>\n      <td>6.51</td>\n      <td>7.21</td>\n      <td>60300.0</td>\n      <td>434763.00</td>\n    </tr>\n    <tr>\n      <td>2022-08-24</td>\n      <td>106.580329</td>\n      <td>107.323567</td>\n      <td>106.134386</td>\n      <td>106.283034</td>\n      <td>16855765.0</td>\n      <td>-0.02</td>\n      <td>120996053.0</td>\n      <td>106.703985</td>\n      <td>106.580329</td>\n      <td>14.86476</td>\n      <td>0.057426</td>\n      <td>交易</td>\n      <td>None</td>\n      <td>0</td>\n      <td>7.89</td>\n      <td>6.45</td>\n      <td>7.17</td>\n      <td>71877.0</td>\n      <td>515358.09</td>\n    </tr>\n    <tr>\n      <td>2022-08-25</td>\n      <td>106.431682</td>\n      <td>107.323567</td>\n      <td>106.283034</td>\n      <td>107.026272</td>\n      <td>15772556.0</td>\n      <td>0.05</td>\n      <td>113287117.0</td>\n      <td>106.766830</td>\n      <td>106.283034</td>\n      <td>14.86476</td>\n      <td>0.053736</td>\n      <td>交易</td>\n      <td>None</td>\n      <td>0</td>\n      <td>7.87</td>\n      <td>6.44</td>\n      <td>7.16</td>\n      <td>57900.0</td>\n      <td>414564.00</td>\n    </tr>\n    <tr>\n      <td>2022-08-26</td>\n      <td>107.472215</td>\n      <td>107.918158</td>\n      <td>106.728977</td>\n      <td>107.323567</td>\n      <td>17576235.0</td>\n      <td>0.02</td>\n      <td>126876441.0</td>\n      <td>107.303290</td>\n      <td>107.026272</td>\n      <td>14.86476</td>\n      <td>0.059881</td>\n      <td>交易</td>\n      <td>None</td>\n      <td>0</td>\n      <td>7.92</td>\n      <td>6.48</td>\n      <td>7.23</td>\n      <td>187100.0</td>\n      <td>1352733.00</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 2
    }
   ],
   "source": [
    "# !!!!!! 每次限8000单元格!!!!\n",
    "# 数据流量的计算方法是滚动计算，从当前时间前推7*24小时，\n",
    "# 这段时间内的用量超过了限制，则需要过一段时间再用，等到按前面所说计算方法计算出来的用量不再超过限制的流量。 \n",
    "# 具体每个函数的流量限制，请联系客户经理咨询\n",
    "\n",
    "# 查询浦发银行，后复权，没有数据填充空值\n",
    "# PriceAdj: 复权，前复权-F，后复权-B，无复权则不写这个参数\n",
    "# 查询字段：\n",
    "# 指标名称    字段\n",
    "# 开盘价      open\n",
    "# 最高价      high\n",
    "# 最低价      low\n",
    "# 收盘价      close\n",
    "# 成交量      volume\n",
    "# 涨跌        chg\n",
    "# 成交额      amt\n",
    "# 均价        vwap\n",
    "# 前收盘价    pre_close\n",
    "# 复权因子    adjfactor    \n",
    "# 换手率      turn\n",
    "# 交易状态    trade_status  \n",
    "# 停牌原因    susp_reason\n",
    "# 涨跌停状态  maxupordown\n",
    "# 涨停价      maxup\n",
    "# 跌停价      maxdown\n",
    "# 开盘集合竞价成交价   open_auction_price   \n",
    "# 开盘集合竞价成交量   open_auction_volume  股（张）\n",
    "# 开盘集合竞价成交额   open_auction_amount  元\n",
    "\n",
    "data_market = w.wsd(\"600000.SH\", \"open,high,low,close,volume,chg,amt,vwap,pre_close,adjfactor,\"\n",
    "                   \"turn,trade_status,susp_reason,maxupordown,maxup,maxdown,open_auction_price,\"\n",
    "                   \"open_auction_volume,open_auction_amount\", \n",
    "      \"2022-08-22\", \"2022-09-22\", \"unit=1;PriceAdj=B\",usedf=True)\n",
    "data_market[1].head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.2 获取日大盘指数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "               CLOSE  PRE_CLOSE\n000001.SH  3088.3695  3108.9096",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>CLOSE</th>\n      <th>PRE_CLOSE</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>000001.SH</td>\n      <td>3088.3695</td>\n      <td>3108.9096</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 2
    }
   ],
   "source": [
    "# 获取上证指数000001.SH，收盘价，前收盘价\n",
    "# priceAdj: 复权类型， cycle：D按日获取\n",
    "errorCode, data=w.wss(\"000001.SH\", \"close,pre_close\",\"tradeDate=20220923;priceAdj=U;cycle=D\", usedf=True)\n",
    "data.head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.3 获取分钟数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "                           open        high         low       close    volume  \\\n2022-09-29 10:00:00  105.391148  105.391148  105.242501  105.391148   64100.0   \n2022-09-29 10:01:00  105.391148  105.539796  105.391148  105.539796  110100.0   \n2022-09-29 10:02:00  105.539796  105.539796  105.242501  105.242501  128300.0   \n2022-09-29 10:03:00  105.391148  105.391148  105.242501  105.391148   12600.0   \n2022-09-29 10:04:00  105.391148  105.391148  105.093853  105.242501  307800.0   \n\n                        amount    change  pctchange  bias_bias    boll_mid  \\\n2022-09-29 10:00:00   454369.0  0.000000   0.000000  -0.070472  105.574099   \n2022-09-29 10:01:00   780685.0  0.148648   0.001410   0.070472  105.562665   \n2022-09-29 10:02:00   909640.0 -0.297295  -0.002817  -0.187970  105.539796   \n2022-09-29 10:03:00    89280.0  0.148648   0.001412  -0.023502  105.522644   \n2022-09-29 10:04:00  2179204.0 -0.148648  -0.001410  -0.141044  105.499775   \n\n                       dmi_pdi  expma_expma      kdj_k       ma_ma    rsi_rsi  \n2022-09-29 10:00:00   5.517682   105.448299  21.623289  105.391148  33.071318  \n2022-09-29 10:01:00  15.589415   105.462375  36.637748  105.420878  66.215718  \n2022-09-29 10:02:00  12.511589   105.428548  24.425166  105.391148  30.255835  \n2022-09-29 10:03:00  11.232215   105.422794  32.950110  105.391148  47.396363  \n2022-09-29 10:04:00   9.087749   105.395057  33.077851  105.361419  36.601897  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>open</th>\n      <th>high</th>\n      <th>low</th>\n      <th>close</th>\n      <th>volume</th>\n      <th>amount</th>\n      <th>change</th>\n      <th>pctchange</th>\n      <th>bias_bias</th>\n      <th>boll_mid</th>\n      <th>dmi_pdi</th>\n      <th>expma_expma</th>\n      <th>kdj_k</th>\n      <th>ma_ma</th>\n      <th>rsi_rsi</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>2022-09-29 10:00:00</td>\n      <td>105.391148</td>\n      <td>105.391148</td>\n      <td>105.242501</td>\n      <td>105.391148</td>\n      <td>64100.0</td>\n      <td>454369.0</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>-0.070472</td>\n      <td>105.574099</td>\n      <td>5.517682</td>\n      <td>105.448299</td>\n      <td>21.623289</td>\n      <td>105.391148</td>\n      <td>33.071318</td>\n    </tr>\n    <tr>\n      <td>2022-09-29 10:01:00</td>\n      <td>105.391148</td>\n      <td>105.539796</td>\n      <td>105.391148</td>\n      <td>105.539796</td>\n      <td>110100.0</td>\n      <td>780685.0</td>\n      <td>0.148648</td>\n      <td>0.001410</td>\n      <td>0.070472</td>\n      <td>105.562665</td>\n      <td>15.589415</td>\n      <td>105.462375</td>\n      <td>36.637748</td>\n      <td>105.420878</td>\n      <td>66.215718</td>\n    </tr>\n    <tr>\n      <td>2022-09-29 10:02:00</td>\n      <td>105.539796</td>\n      <td>105.539796</td>\n      <td>105.242501</td>\n      <td>105.242501</td>\n      <td>128300.0</td>\n      <td>909640.0</td>\n      <td>-0.297295</td>\n      <td>-0.002817</td>\n      <td>-0.187970</td>\n      <td>105.539796</td>\n      <td>12.511589</td>\n      <td>105.428548</td>\n      <td>24.425166</td>\n      <td>105.391148</td>\n      <td>30.255835</td>\n    </tr>\n    <tr>\n      <td>2022-09-29 10:03:00</td>\n      <td>105.391148</td>\n      <td>105.391148</td>\n      <td>105.242501</td>\n      <td>105.391148</td>\n      <td>12600.0</td>\n      <td>89280.0</td>\n      <td>0.148648</td>\n      <td>0.001412</td>\n      <td>-0.023502</td>\n      <td>105.522644</td>\n      <td>11.232215</td>\n      <td>105.422794</td>\n      <td>32.950110</td>\n      <td>105.391148</td>\n      <td>47.396363</td>\n    </tr>\n    <tr>\n      <td>2022-09-29 10:04:00</td>\n      <td>105.391148</td>\n      <td>105.391148</td>\n      <td>105.093853</td>\n      <td>105.242501</td>\n      <td>307800.0</td>\n      <td>2179204.0</td>\n      <td>-0.148648</td>\n      <td>-0.001410</td>\n      <td>-0.141044</td>\n      <td>105.499775</td>\n      <td>9.087749</td>\n      <td>105.395057</td>\n      <td>33.077851</td>\n      <td>105.361419</td>\n      <td>36.601897</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 2
    }
   ],
   "source": [
    "# 基本行情指标名称\n",
    "# 开盘价   open\n",
    "# 最高价   high\n",
    "# 最低价   low\n",
    "# 收盘价   close\n",
    "# 成交量   volume\n",
    "# 成交额   amt\n",
    "# 涨跌     chg\n",
    "# 涨跌幅   pct_chg\n",
    "\n",
    "# 技术指标       字段      参数\n",
    "# BIAS乖离率    BIAS      12\n",
    "# BOLL布林带    BOLL      26;2;MID\n",
    "# DMI趋向标准   DMI       14;6;PDI\n",
    "# EXPMA指数滑动平均   EXPMA   12\n",
    "# KDJ随机指标   KDJ       9;3;3;k\n",
    "# MA简单移动平均 MA       5\n",
    "# RSI相对强弱指标  RSI    6\n",
    "\n",
    "# 获取后复权，1分钟间隔数据\n",
    "# 默认获取1分钟数据，可以不舍参数，获取具体分钟间隔，通过BarSize设置\n",
    "# w.wsi(\"600000.SH\", \"open,high,low,close,volume,amt,chg,pct_chg\", \"2022-09-29 10:00:00\", \n",
    "#   \"2022-09-29 10:15:00\", \"BarSize=3;PriceAdj=B\")\n",
    "\n",
    "errorCode, data=w.wsi(\"600000.SH\", \"open,high,low,close,volume,amt,chg,pct_chg,BIAS,BOLL,DMI,EXPMA,KDJ,MA,RSI\", \n",
    "      \"2022-09-29 10:00:00\", \"2022-09-29 10:15:00\", \n",
    "      \"BIAS_N=12;BOLL_N=26;BOLL_Width=2;BOLL_IO=1;DMI_N=14;DMI_N1=6;DMI_IO=1;EXPMA_N=12;KDJ_N=9;\"\n",
    "      \"KDJ_M1=3;KDJ_M2=3;KDJ_IO=1;MA_N=5;RSI_N=6;PriceAdj=B\", usedf=True)\n",
    "data.head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5. 获取交易日列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "                     \n2022-08-29 2022-08-29\n2022-08-30 2022-08-30\n2022-08-31 2022-08-31\n2022-09-01 2022-09-01\n2022-09-02 2022-09-02",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>2022-08-29</td>\n      <td>2022-08-29</td>\n    </tr>\n    <tr>\n      <td>2022-08-30</td>\n      <td>2022-08-30</td>\n    </tr>\n    <tr>\n      <td>2022-08-31</td>\n      <td>2022-08-31</td>\n    </tr>\n    <tr>\n      <td>2022-09-01</td>\n      <td>2022-09-01</td>\n    </tr>\n    <tr>\n      <td>2022-09-02</td>\n      <td>2022-09-02</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 7
    }
   ],
   "source": [
    "errorCode, data = w.tdays(\"2022-08-28\", \"2022-09-28\", \"\", usedf=True)\n",
    "data.head(5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}